// pages/regimental-commander/order-refund/order-refund.js
import Toast from '../../../dist/toast/toast'
import Dialog from '../../../dist/dialog/dialog'
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    product: null,
    refundForm: {
      total: 0,
      status: '',
      reason: '',
      money: '',
      desc: '',
      img: []
    },
    uploadFileUrl: 'upload.uploadPic',
    refundTotal: 0,
    statusShow: false,
    reasonShow: false,
    piecesShow: false,
    moneyErrorText: '',
    receivingStatusAction: [
      { name: '司机未到自提点' },
      { name: '司机送达，该商品缺货' },
      { name: '货物在自提点，顾客未提货' },
      { name: '顾客已提货' },
      { name: '顾客已退货到自提点' }
    ],
    reasonAction: [
      { name: '已化冻影响食用' },
      { name: '冻伤/冻坏' },
      { name: '质量问题' },
      { name: '货物与描述不符' },
      { name: '商品破损/压坏' },
      { name: '商家发错货' },
      { name: '少发、漏发' },
      { name: '缺货' },
      { name: '不想要了' }
    ]
  },

  /**
   * 件数改变
   * @param {Object} e 
   */
  refundTotalChange: function(e) {
    this.setData({
      refundTotal: e.detail
    })
  },

  /**
   * 设置退货件数
   */
  setFormTotal: function() {
    const {refundTotal} = this.data
    if (refundTotal < 1) {
      Toast('请设置退货件数')
      return
    }
    this.setData({
      'refundForm.total': refundTotal,
      'refundForm.money': refundTotal * this.data.product.price,
      moneyErrorText: '',
      piecesShow: false
    })
  },

  /**
   * 校验申请金额
   * @param {Object} e 
   */
  refundMoneyChange: function(e) {
    if (!this.data.refundForm.total) {
      return
    }
    const money = Number(e.detail)
    if (isNaN(money) || money < 0) {
      this.setData({
        moneyErrorText: '输入错误'
      })
      return
    } 
    if (money > this.data.refundForm.total * this.data.product.price) {
      this.setData({
        moneyErrorText: `您申请退${this.data.refundForm.total}件，最多可申请退¥${this.data.refundForm.total * this.data.product.price}`
      })
    } else {
      this.setData({
        moneyErrorText: ''
      })
    }
  },

  /**
   * 图片选中上传(多图)
   * @param {Object} e 
   */
  afterRead: function(e) {
    const { file = [] } = e.detail
    let promiseArray = []
    const uploaderUrl = app.getNetAddresss(this.data.uploadFileUrl)
    wx.showLoading({
      title: '上传中',
    })
    file.forEach(item => {
      promiseArray.push(new Promise((resolve, reject) => {
        wx.uploadFile({
          filePath: item.url,
          name: 'file',
          url: uploaderUrl,
          success: function(res) {
            resolve(res)
          },
          fail: function(err) {
            reject(err)
          }
        })
      }))
    })
    Promise.all(promiseArray).then(resArray => {
      wx.hideLoading()
      const addUrlArray = resArray.map(item => {
        const resData = JSON.parse(item.data)
        return {url: resData.data.img_url}
      })
      const currentImgUrls = [...this.data.refundForm.img, ...addUrlArray]
      this.setData({
        'refundForm.img': currentImgUrls
      })
      console.log(this.data.refundForm.img)
    }).catch(err => {
      wx.hideLoading()
    })
  },

  /**
   * 删除已上传的图片
   * @param {Object} e 
   */
  deleteFile: function(e) {
    const { index } = e.detail
    let currentData = this.data.refundForm.img
    currentData.splice(index, 1)
    this.setData({
      'refundForm.img': currentData
    })
  },
  
  /**
   * 动作面板选中
   * @param {Object} e 
   */
  actionSheetSelected: function(e) {
    const { item, sheetType, formType } = e.currentTarget.dataset
    let form = this.data.refundForm
    form[formType] = item
    console.log(form)
    this.setData({
      refundForm: form,
      [sheetType]: false
    })
  },

  /**
   * 显示动作面板
   * @param {Object} e 
   */
  showActionSheet: function(e) {
    const type = e.currentTarget.dataset.sheetType
    if (this.data.product.total < 2 && type == 'piecesShow') {
      return
    }
    this.setData({
      [type]: true
    })
  },

  /**
   * 隐藏动作面板
   * @param {Object} e 
   */
  hideActionSheet: function(e) {
    const type = e.currentTarget.dataset.sheetType
    this.setData({
      [type]: false
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    if (!options.params) {
      Dialog.alert({
        title: '错误',
        message: '无效的访问',
      }).then(() => {
        wx.navigateBack({
          delta: 1
        })
      })
      return
    }
    const params = decodeURIComponent(options.params)
    const product = JSON.parse(params)
    this.setData({product})
    if (product.total == 1) {
      this.setData({
        'refundForm.total': 1,
        'refundForm.money': product.price
      })
    } else {
      this.setData({
        piecesShow: true
      })
    }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})